package SwingView.Gradebook;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jdesktop.swingx.JXDatePicker;

import SwingController.CmsPresenter;
import SwingModel.CmsAssignment;
import SwingModel.CmsGradebook;
import SwingView.CmsContentPanelModel;
import SwingView.CmsTimeSelector;
import javax.swing.border.EmptyBorder;

public class CmsCPStudentGradebook extends CmsContentPanelModel{

	private final JLabel jlAsgmEditTitle;
	private JTextField jtfTitle;
	private JTextArea jtfDescription;
	private CmsGradebook gradebook;
	private JLabel lblModifiedGradesAre;
	private final JPanel jpContent;

	
	public CmsCPStudentGradebook(){
		
		//helper panel to center the title panel
		JPanel jp = new JPanel();
		jp.setPreferredSize(new Dimension(650, 65));
		jp.setOpaque(false);
		jp.setBorder(BorderFactory.createEmptyBorder());
		GridBagLayout gbl_jp = new GridBagLayout();
		jp.setLayout(gbl_jp);
		
		//content panel
		jpContent = new JPanel();
		GridBagLayout gbl_jpContent = new GridBagLayout();
		jpContent.setLayout(gbl_jpContent);
		jpContent.setOpaque(false);
		
		//Set up the scroll bar
		JScrollPane scroll = new JScrollPane(jpContent);
		
		//main Scroll bar
		scroll.setBorder(BorderFactory.createEmptyBorder());
		scroll.setOpaque(true);
		scroll.setPreferredSize(new Dimension(650,450));
		scroll.setBackground(Color.WHITE);

		//set up main panel and add components
		setLayout(new BorderLayout());
		setOpaque(false);
		add(jp,BorderLayout.NORTH);//Add title People
		
		//instantiate Quiz title
		jlAsgmEditTitle = new JLabel("Gradebook");
		jlAsgmEditTitle.setFont(jlAsgmEditTitle.getFont().deriveFont(20.0f).deriveFont(Font.BOLD));
		jlAsgmEditTitle.setBorder(new EmptyBorder(0, 8, 5, 8));
		GridBagConstraints gbc_jlQuizComposerTitle = new GridBagConstraints();
		gbc_jlQuizComposerTitle.insets = new Insets(0, 0, 5, 0);
		gbc_jlQuizComposerTitle.gridx = 0;
		gbc_jlQuizComposerTitle.gridy = 0;
		jp.add(jlAsgmEditTitle, gbc_jlQuizComposerTitle);
		
		add(scroll,BorderLayout.WEST);//Add People List
	}
	
	public void loadGradebook(CmsGradebook gradebook){
		jpContent.removeAll();
		Object[] gradeData = gradebook.data.get(0);
		String[] courseWork = gradebook.collumnNames;
		
		for(int i = 1; i < (gradeData.length - 1); i++){ //exclude the total
			JLabel lbCourseWork = new JLabel("" + courseWork[i]);
			lbCourseWork.setFont(new Font("Lucida Grande", Font.BOLD, 15));
			lbCourseWork.setBorder(BorderFactory.createEmptyBorder(3, 15, 3, 15));
			GridBagConstraints c = new GridBagConstraints();
			c.gridx = 0;
			c.gridy = i;
			c.insets = new Insets(0,60,0,0);
			c.anchor = GridBagConstraints.FIRST_LINE_START;
			c.fill = GridBagConstraints.HORIZONTAL;
			c.weightx = 1.0;
			jpContent.add(lbCourseWork,c);
			
			JLabel lbGrade = new JLabel("" + gradeData[i]);
			lbGrade.setBorder(BorderFactory.createEmptyBorder(3, 15, 3, 15));
			lbGrade.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
			c = new GridBagConstraints();
			c.gridx = 1;
			c.gridy = i;
			c.insets = new Insets(0,0,5,40);
			c.anchor = GridBagConstraints.FIRST_LINE_END;
			c.fill = GridBagConstraints.HORIZONTAL;
			jpContent.add(lbGrade,c);
			
			if((i%2)==0){
				lbCourseWork.setOpaque(true);
				lbCourseWork.setBackground(new Color(200,200,200));
				lbGrade.setOpaque(true);
				lbGrade.setBackground(new Color(200,200,200));
			}
			else{
				lbCourseWork.setOpaque(true);
				lbCourseWork.setBackground(new Color(230,230,230));
				lbGrade.setOpaque(true);
				lbGrade.setBackground(new Color(230,230,230));
			}
		}
		
		//Add the Total:
		JLabel lbCourseWork = new JLabel("" + courseWork[courseWork.length-1]);
		lbCourseWork.setBorder(BorderFactory.createEmptyBorder(3, 15, 3, 15));
		lbCourseWork.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		lbCourseWork.setOpaque(true);
		lbCourseWork.setBackground(Color.BLACK);
		lbCourseWork.setForeground(Color.WHITE);
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = courseWork.length-1;
		c.insets = new Insets(0,60,0,0);
		c.anchor = GridBagConstraints.FIRST_LINE_START;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1.0;
		jpContent.add(lbCourseWork,c);
		
		JLabel lbGrade = new JLabel("" + gradeData[gradeData.length-1]);
		lbGrade.setBorder(BorderFactory.createEmptyBorder(3, 15, 3, 15));
		lbGrade.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		lbGrade.setOpaque(true);
		lbGrade.setBackground(Color.BLACK);
		lbGrade.setForeground(Color.WHITE);
		c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = gradeData.length-1;
		c.insets = new Insets(0,0,5,40);
		c.anchor = GridBagConstraints.FIRST_LINE_END;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weighty = 1.0;
		jpContent.add(lbGrade,c);
		
		jpContent.revalidate();
		jpContent.repaint();
	}

}
